package com.example.demo.demos.web;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.FileOutputStream;

public class WordExample {
    public static void main(String[] args) {
        try {
            // 创建一个新的Word文档
            XWPFDocument document = new XWPFDocument();

            // 创建一个新段落
            XWPFParagraph paragraph = document.createParagraph();

            // 设置段落样式为"标题一"
            paragraph.setStyle("Heading1");

            // 创建一个Run对象并添加文本
            XWPFRun run = paragraph.createRun();
            run.setText("这是一段标题一的文本");

            // 将文档写入输出流
            FileOutputStream out = new FileOutputStream("example.docx");
            document.write(out);
            out.close();
            document.close();

            System.out.println("Word文档已成功创建。");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
